package xcmg.device.web.agent;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.agent.ServiceBasicDataAgentService;
import xcmg.device.service.vo.agent.ServiceBasicDataAgentVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.List;

/**
 * ServiceBasicDataAgent Controller 实现类
 *
 * @author huhuawei
 */
@RestController
@RequestMapping("/serviceBasicDataAgent")
public class ServiceBasicDataAgentController extends BasicController {
    private static final Logger logger = LoggerFactory.getLogger(ServiceBasicDataAgentController.class);
    @Autowired
    private ServiceBasicDataAgentService serviceBasicDataAgentService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "代理商数据字典分页列表信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult<PageInfo<ServiceBasicDataAgentVO>> list(@RequestBody SearchCommonVO<ServiceBasicDataAgentVO> condition) throws Exception {
        PageCommonVO<ServiceBasicDataAgentVO> pageCommonVO = serviceBasicDataAgentService.list(condition,getOrgId());
        return actionValueResult(pageCommonVO.getPageInfo());
    }


    @RequestMapping(value = "/searchList", method = RequestMethod.POST)
    @ApiOperation(value = "代理商数据字典列表信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult<List<ServiceBasicDataAgentVO>> searchList(@RequestBody ServiceBasicDataAgentVO condition) throws Exception {
        condition.setOrgId(getOrgId());
        return actionValueResult(serviceBasicDataAgentService.queryList(condition));
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "代理商数据字典-保存")
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> save(@RequestBody ServiceBasicDataAgentVO serviceBasicDataAgentVO) {
        if (null == serviceBasicDataAgentVO) {
            return actionResult(ErrorCode.IllegalArument);
        }
        try {
            ErrorCode code = serviceBasicDataAgentService.save(serviceBasicDataAgentVO, getUserId(), getUserName(), getOrgId());
            return actionResult(code);
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }


    @RequestMapping(value = "/item", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询代理商数据字典详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult<ServiceBasicDataAgentVO> item(String id) {
        ServiceBasicDataAgentVO serviceBasicDataAgentVO = serviceBasicDataAgentService.queryById(id);
        return actionValueResult(serviceBasicDataAgentVO);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除代理商数据字典数据")
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> delete(@RequestBody ServiceBasicDataAgentVO serviceBasicDataAgentVO) {
        ErrorCode errorCode = null;
        try {
            errorCode = serviceBasicDataAgentService.batchDelete(serviceBasicDataAgentVO.getListId());
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
        return actionResult(errorCode);
    }
}
